package lumis.service.wsrp.producer;

import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.sun.portal.container.ContainerFactory;
import com.sun.portal.container.ContainerType;
import com.sun.portal.wsrp.common.WSRPInitializer;

/**
 * Initializes the WSRP layer.
 *
 * @version $Revision: 12680 $ $Date: 2011-03-04 15:32:40 -0300 (Fri, 04 Mar 2011) $
 * @since 5.5.0
 */
public class WSRPContextListener extends WSRPInitializer implements ServletContextListener
{
	public void contextInitialized(ServletContextEvent event)
	{
		try
		{
			ClassPool classPool = new ClassPool();
			classPool.insertClassPath(new ClassClassPath(getClass()));
			
			// fix for WSRPConsumerRewriter.getResourceTemplate using V1 - 
			// must behave as V2 to use Lumis Portal ChannelURLFactory implementation
			CtClass ctClass = classPool.get("com.sun.portal.wsrp.consumer.markup.WSRPConsumerRewriter");
			CtMethod ctMethod = ctClass.getDeclaredMethod("getResourceTemplate");
			ctMethod.insertBefore("$2 = com.sun.portal.wsrp.common.WSRPVersion.VERSION2.value();");
			ctClass.toClass();
			
			//fix to stop ServiceFinder from trying to use classes in weblogic.jar file for xml based services.
			CtClass iteratorClass = classPool.get("com.sun.xml.ws.util.ServiceFinder$LazyIterator");
			CtMethod hasNextMethod = iteratorClass.getDeclaredMethod("hasNext");
			hasNextMethod.insertBefore("{if (this.configs == null) { String fullName = \"META-INF/services/\" + this.service.getName(); " +
					" if (this.loader == null) this.configs = ClassLoader.getSystemResources(fullName); else{ " +
					" java.util.List resources = java.util.Collections.list(this.loader.getResources(fullName));" +
					" for (java.util.Iterator iterator = resources.iterator(); iterator.hasNext();)" +
					" {java.net.URL url = (java.net.URL) iterator.next();if(url.toString().contains(\"weblogic.jar!\")){iterator.remove();}}" +
					" this.configs = java.util.Collections.enumeration(resources); " +
					" } } }");
			iteratorClass.toClass();
			
			// disable DumpFilter.process (was outputting all consumer SOAP messages)
			CtClass dumpFilterClass = classPool.get("com.sun.xml.wss.impl.filter.DumpFilter");
			CtMethod processMethod = dumpFilterClass.getDeclaredMethod("process");
			processMethod.insertBefore("return;");
			dumpFilterClass.toClass();
			

			// Validate publish portlets according to our object model
			CtClass markUpManagerClass = classPool.get("com.sun.portal.wsrp.producer.markup.impl.MarkupManagerImpl");
			CtMethod validatePortletHandleMethod = markUpManagerClass.getDeclaredMethod("validatePortletHandle");
			validatePortletHandleMethod.insertBefore("lumis.service.wsrp.producer.WSRPProducerUtils.validate(_producer, portletHandle);return;");
			markUpManagerClass.toClass();
			
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		
		super.contextInitialized(event);
		ContainerFactory.setContainer(ContainerType.PORTLET_CONTAINER, new ProducerContainer());
	}

	public void contextDestroyed(ServletContextEvent event)
	{
		super.contextDestroyed(event);
	}
}
